package com.google.gwt.it2tool.server;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashMap;

import javax.jdo.annotations.IdGeneratorStrategy;
import javax.jdo.annotations.PersistenceCapable;
import javax.jdo.annotations.Persistent;
import javax.jdo.annotations.PrimaryKey;

import com.google.appengine.api.datastore.Key;

@PersistenceCapable
public class RecipeJDO implements Serializable{
	
	@PrimaryKey
	@Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY)
	private Key key;

	@Persistent
	private String name;

	@Persistent
	private ArrayList<String> ingridients = new ArrayList<String>();
	
	@Persistent
	private ArrayList<Double> amounts = new ArrayList<Double>();
	
	@Persistent
	private ArrayList<String> units = new ArrayList<String>();
	

	public RecipeJDO(String name){
		this.name=name;
	}
	
	public RecipeJDO(String name, HashMap<String,Double> ingridients, HashMap<String,String> units) {
		this.name = name;

		for(String ing: ingridients.keySet()){
			this.ingridients.add(ing);
			amounts.add(ingridients.get(ing));
			this.units.add(units.get(ing));
		}
	}

	public Key getKey() {
		return key;
	}

	public void setKey(Key key) {
		this.key = key;
	}

	public ArrayList<Double> getAmounts() {
		return amounts;
	}

	public void setAmounts(ArrayList<Double> amounts) {
		this.amounts = amounts;
	}

	public void setUnits(ArrayList<String> units) {
		this.units = units;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public ArrayList<String> getIngridients() {
		return ingridients;
	}

	public ArrayList<String> getUnits() {
		return units;
	}
	
	public void setIngridients(HashMap<String, Double> ingridients, HashMap<String, String> units) {
		this.ingridients = new ArrayList<String>();
		this.units = new ArrayList<String>(); 
		for(String ing: ingridients.keySet()){
			this.ingridients.add(ing);
			amounts.add(ingridients.get(ing));
			this.units.add(units.get(ing));
		}
	}

	public void setIngridients(ArrayList<String> ingridients) {
		this.ingridients = ingridients;
	}
	
	public void addIngridient(String name, double amount, String unit){
		ingridients.add(name);
		amounts.add(amount);
		units.add(unit);
	}	
	
	public HashMap<String,Double> getIngridientsAsHashmap(){
		HashMap<String,Double> output = new HashMap<String, Double>();
		for(String s: ingridients){
			output.put(s, amounts.get(ingridients.indexOf(s)));
		}		
		return output;
	}
	
	public HashMap<String,String> getUnitsAsHashmap(){
		HashMap<String,String> output = new HashMap<String, String>();
		for(String s: ingridients){
			output.put(s, units.get(ingridients.indexOf(s)));
		}		
		return output;
	}
}
